package cn.yj.quartz.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 注入数据源，使用项目中的数据源
 *  关于 SchedulerFactoryBean 创建也可以通过@Bean 的方式，然后手动配置properties和数据源
 * @author 永健
 * @since 2022-01-14 16:01
 */
@Component
public class SchedulerFactoryBeanConfig implements BeanPostProcessor {

    @Resource
    private DataSource dataSource;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof SchedulerFactoryBean) {
            SchedulerFactoryBean schedulerFactoryBean = (SchedulerFactoryBean) bean;
            schedulerFactoryBean.setDataSource(dataSource);
            return schedulerFactoryBean;
        }
        return bean;
    }
}
